package admin.controllers;

import helpers.IdUniqueHelper;

import java.io.IOException;
import java.net.URLDecoder;
import java.util.List;
import java.util.TreeMap;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import search.ArticleSearchEngine;

import admin.forms.ArticleForm;
import admin.models.ApplicationModel;
import admin.models.ArticleModel;
import admin.models.CategoryModel;

import entities.Application;
import entities.Article;
import entities.ArticleSearch;
import entities.Category;

@SuppressWarnings("serial")
public class ArticleAddServlet extends HttpServlet {
	public void doGet(HttpServletRequest request, HttpServletResponse response)
		throws IOException, ServletException {
		TreeMap<String, String> check = new TreeMap<String, String>();
		
		String path = ((HttpServletRequest) request).getRequestURI();
		String id = URLDecoder.decode(path.replaceAll(request.getServletPath() + "/", ""),"UTF-8");
		
		if (id == null || id.isEmpty()) {
			response.sendRedirect("/admin/application/list");
			return ;
		}
		
		ApplicationModel appModel = ApplicationModel.getInstance();
		Application app = appModel.get(id);
		
		if (app == null) {
			response.sendRedirect("/admin/application/list");
			return ;
		}
		
		CategoryModel categoryModel = CategoryModel.getInstance();
		List<Category> categories = (List<Category>) categoryModel.getAll(app.getId());
		
		ArticleModel articleModel = ArticleModel.getInstance();
		Article article = new Article();
		
		request.setAttribute("form", "add");
		request.setAttribute("check", check);
		request.setAttribute("article", article);
		request.setAttribute("app", app);
		request.setAttribute("categories", categories);
		try {
			request.getRequestDispatcher("/admin/pages/article_form.jsp").forward(request, response);
		} catch (ServletException e) {
			e.printStackTrace();
		}
	}
	
	public void doPost(HttpServletRequest request, HttpServletResponse response)
		throws IOException, ServletException {
		TreeMap<String, String> check = new TreeMap<String, String>();
		
		String path = ((HttpServletRequest) request).getRequestURI();
		String id = URLDecoder.decode(path.replaceAll(request.getServletPath() + "/", ""),"UTF-8");
		
		if (id == null || id.isEmpty()) {
			response.sendRedirect("/admin/application/list");
			return ;
		}
		
		ApplicationModel appModel = ApplicationModel.getInstance();
		Application app = appModel.get(id);
		
		if (app == null) {
			response.sendRedirect("/admin/application/list");
			return ;
		}
		
		CategoryModel categoryModel = CategoryModel.getInstance();
		List<Category> categories = (List<Category>) categoryModel.getAll(app.getId());
		
		ArticleModel articleModel = ArticleModel.getInstance();
		Article article = new Article();
		
		request.setAttribute("appId", id);
		
		TreeMap<String, Object> form = ArticleForm.checkValid(request, article);
		article = (Article) form.get("article");
		check = (TreeMap<String, String>) form.get("checkValid");
		
		if (check.size() == 0) {
			article.setId(IdUniqueHelper.getId());
			articleModel.insert(app.getId(), article);
			
			request.setAttribute("success", "Creat new article " + app.getId() + " success");
			
			article = new Article();
		}
		
		request.setAttribute("form", "add");
		request.setAttribute("check", check);
		request.setAttribute("article", article);
		request.setAttribute("app", app);
		request.setAttribute("categories", categories);
		try {
			request.getRequestDispatcher("/admin/pages/article_form.jsp").forward(request, response);
		} catch (ServletException e) {
			e.printStackTrace();
		}
	}
}
